/*
 * Copyright (c) 2007, Nubo Technologies
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.nubotech.gwt.oss.client;

import com.google.gwt.core.client.GWT;
import com.nubotech.gwt.oss.client.s3.S3FileUploadFormPanel;

/**
 *
 * @author jonnakkerud
 */
public class Condition {
    
    private String name;
    private String value;
    private MatchType matchType = EXACT;
    
    public static class MatchType {
        private String type;
        private MatchType(String type) {
            this.type = type;
        }
        
        public String typeName() {
            return type;
        }
    }
    
    public static final MatchType EXACT = new MatchType(null);
    public static final MatchType STARTS_WITH = new MatchType("starts-with");
    public static final MatchType CONTENT_LENGTH_RANGE = new MatchType("content-length-range");    
    
    public Condition() {
    }

    public Condition(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public Condition(MatchType matchType, String name, String value) {
        this.matchType = matchType;
        this.name = name;
        this.value = value;
    }

    public Condition(String[] range) {
        this.matchType = CONTENT_LENGTH_RANGE;
        this.name = range[0];
        this.value = range[1];
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public MatchType getMatchType() {
        return matchType;
    }

    public void setMatchType(MatchType matchType) {
        this.matchType = matchType;
    }
    
    public String json() {
        StringBuffer sb = new StringBuffer();
        if (matchType == null || matchType.typeName() == null) {
            sb.append("{");            
            sb.append(quote(this.name)).append(":").append(quote(this.value));
            sb.append("}");
        }
        else {
            sb.append("[");            
            sb.append(quote(this.matchType.typeName()));
            sb.append(",");
            sb.append(quote(this.name));
            sb.append(",");
            sb.append(quote(this.value));            
            sb.append("]");            
        }
        return sb.toString();
    }
    
    public static String quote(String value) {
        StringBuffer sb = new StringBuffer();
        sb.append('"').append(value).append('"');
        return sb.toString();
    }
    
    
    public static Condition createCondition(String key, String value) {
        Condition condition = null;
        if ("key".equals(key)) {
            // remove ${filename}
            if (value != null && value.indexOf(S3FileUploadFormPanel.FILENAME_VAR) > -1) {
                value = value.substring(0, value.indexOf(S3FileUploadFormPanel.FILENAME_VAR));
                GWT.log(value, null);
            }
            condition = new Condition(Condition.STARTS_WITH, "$"+key, value);
        }
        else if ("Content-Type".equals(key)) {
            condition = new Condition(Condition.STARTS_WITH, "$"+key, value);            
        }
        else {
            condition = new Condition(key, value);
        }
        return condition;
    }
    
}
